package test0ne.多线程;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 模逆一个多线程并发，生产者消费者模式
 */
public class ProviderAndConsumer {

    private static BlockingDeque<String> queue = new LinkedBlockingDeque<String>(5);

    private static class Productor extends Thread {

        @Override
        public void run() {
            try {
                queue.put("product");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("productor...");
        }
    }


    private static class Consumer extends Thread {

        public void run() {
            try {
                String product = queue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Consumer");
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Productor productor = new Productor();
            productor.start();
        }

        for (int i = 0; i < 20; i++) {
            Consumer consumer = new Consumer();
            consumer.start();
        }
    }
}
